package com.ds.msg.mqtt.command.filter;

import com.ds.common.JDSConstants;
import com.ds.common.logging.Log;
import com.ds.common.logging.LogFactory;
import com.ds.common.md5.MD5;
import com.ds.engine.ConnectInfo;
import com.ds.engine.JDSSessionHandle;
import com.ds.msg.Msg;
import com.ds.msg.MsgFactroy;
import com.ds.msg.filter.MsgFilterChain;
import com.ds.org.Person;
import com.ds.server.JDSServer;
import com.ds.server.OrgManagerFactory;

import java.io.File;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.Callable;

public class SendMsgChain implements Callable<Boolean> {

    private Msg msg;
    private static final Log logger = LogFactory.getLog(JDSConstants.CONFIG_KEY, SendMsgChain.class);

    public SendMsgChain(Msg msg) {
        this.msg = msg;
    }

    public Boolean call() throws Exception {
        Boolean isSuccess = false;

        try {
            String receiver = msg.getReceiver();
            Person person = OrgManagerFactory.getOrgManager().getPersonByID(receiver);
            ConnectInfo connectInfo = new ConnectInfo(person.getID(), person.getAccount(), person.getPassword());
            Set<JDSSessionHandle> sessionHandleList = JDSServer.getInstance().getSessionHandleList(connectInfo);
            for (JDSSessionHandle sessionHandle : sessionHandleList) {
                MsgFilterChain chain = new MsgFilterChain();
                chain.addFilter(new MsgFilterImpl());
                chain.addFilter(new CommandFilterImpl());
                isSuccess = chain.filterObject(msg, sessionHandle);
            }
            MsgFactroy.getInstance().getClient(null, Msg.class).updateMsg(msg);


        } catch (Exception e) {

            e.printStackTrace();
        }

        return isSuccess;
    }

    public static void main(String[] args) {
        File file = new File("C:\\Users\\wenzhang\\Desktop\\smart.tujia.com_3.6.8.bin");
        try {
            System.out.println(MD5.getHashString(file).toString());
            System.out.println(file.length());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
